package o;

import android.text.TextUtils;
import com.huawei.appgallery.base.httpskit.PostField;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import org.json.JSONException;

public final class bjt {
    public static String ˋ(Object obj) throws JSONException {
        if (obj == null) {
            return HwAccountConstants.NULL;
        }
        if (obj instanceof String) {
            return ˏ((String) obj);
        }
        if (obj instanceof Boolean) {
            return ॱ((Boolean) obj);
        }
        if (obj instanceof Number) {
            return ॱ((Number) obj);
        }
        if (obj instanceof Map) {
            return ˎ((Map) obj);
        }
        if (obj instanceof Collection) {
            return ॱ((Collection) obj);
        }
        if (obj instanceof Object[]) {
            return ˎ((Object[]) obj);
        }
        if (obj instanceof int[]) {
            return ˎ((int[]) obj);
        }
        if (obj instanceof boolean[]) {
            return ˊ((boolean[]) obj);
        }
        if (obj instanceof long[]) {
            return ˊ((long[]) obj);
        }
        if (obj instanceof float[]) {
            return ˋ((float[]) obj);
        }
        if (obj instanceof double[]) {
            return ˊ((double[]) obj);
        }
        if (obj instanceof short[]) {
            return ˏ((short[]) obj);
        }
        if (obj instanceof byte[]) {
            return ˋ((byte[]) obj);
        }
        return ˊ(obj);
    }

    static String ˏ(String str) {
        StringBuilder stringBuilder = new StringBuilder(str.length() + 20);
        stringBuilder.append('\"');
        for (int i = 0; i < str.length(); i++) {
            char charAt = str.charAt(i);
            switch (charAt) {
                case '\b':
                    stringBuilder.append("\\b");
                    break;
                case '\t':
                    stringBuilder.append("\\t");
                    break;
                case '\n':
                    stringBuilder.append("\\n");
                    break;
                case '\f':
                    stringBuilder.append("\\f");
                    break;
                case '\r':
                    stringBuilder.append("\\r");
                    break;
                case '\"':
                    stringBuilder.append("\\\"");
                    break;
                case '/':
                    stringBuilder.append("\\/");
                    break;
                case '\\':
                    stringBuilder.append("\\\\");
                    break;
                default:
                    stringBuilder.append(charAt);
                    break;
            }
        }
        stringBuilder.append('\"');
        return stringBuilder.toString();
    }

    static String ॱ(Number number) {
        return number.toString();
    }

    static String ॱ(Boolean bool) {
        return bool.toString();
    }

    static String ॱ(Collection<Object> collection) throws JSONException {
        return ˋ(collection.toArray());
    }

    static String ˎ(Map<String, Object> map) throws JSONException {
        if (map.isEmpty()) {
            return "{}";
        }
        StringBuilder stringBuilder = new StringBuilder(map.size() << 4);
        stringBuilder.append('{');
        for (Entry entry : map.entrySet()) {
            Object value = entry.getValue();
            stringBuilder.append('\"');
            stringBuilder.append(entry.getKey());
            stringBuilder.append('\"');
            stringBuilder.append(':');
            stringBuilder.append(ˋ(value));
            stringBuilder.append(',');
        }
        stringBuilder.setCharAt(stringBuilder.length() - 1, '}');
        return stringBuilder.toString();
    }

    static String ˎ(Object[] objArr) throws JSONException {
        if (objArr.length == 0) {
            return "[]";
        }
        StringBuilder stringBuilder = new StringBuilder(objArr.length << 4);
        stringBuilder.append('[');
        for (Object ˋ : objArr) {
            stringBuilder.append(ˋ(ˋ));
            stringBuilder.append(',');
        }
        stringBuilder.setCharAt(stringBuilder.length() - 1, ']');
        return stringBuilder.toString();
    }

    static String ˎ(int[] iArr) {
        if (iArr.length == 0) {
            return "[]";
        }
        StringBuilder stringBuilder = new StringBuilder(iArr.length << 4);
        stringBuilder.append('[');
        for (int num : iArr) {
            stringBuilder.append(Integer.toString(num));
            stringBuilder.append(',');
        }
        stringBuilder.setCharAt(stringBuilder.length() - 1, ']');
        return stringBuilder.toString();
    }

    static String ˊ(long[] jArr) {
        if (jArr.length == 0) {
            return "[]";
        }
        StringBuilder stringBuilder = new StringBuilder(jArr.length << 4);
        stringBuilder.append('[');
        for (long l : jArr) {
            stringBuilder.append(Long.toString(l));
            stringBuilder.append(',');
        }
        stringBuilder.setCharAt(stringBuilder.length() - 1, ']');
        return stringBuilder.toString();
    }

    static String ˊ(boolean[] zArr) {
        if (zArr.length == 0) {
            return "[]";
        }
        StringBuilder stringBuilder = new StringBuilder(zArr.length << 4);
        stringBuilder.append('[');
        for (boolean bool : zArr) {
            stringBuilder.append(Boolean.toString(bool));
            stringBuilder.append(',');
        }
        stringBuilder.setCharAt(stringBuilder.length() - 1, ']');
        return stringBuilder.toString();
    }

    static String ˋ(float[] fArr) {
        if (fArr.length == 0) {
            return "[]";
        }
        StringBuilder stringBuilder = new StringBuilder(fArr.length << 4);
        stringBuilder.append('[');
        for (float f : fArr) {
            stringBuilder.append(Float.toString(f));
            stringBuilder.append(',');
        }
        stringBuilder.setCharAt(stringBuilder.length() - 1, ']');
        return stringBuilder.toString();
    }

    static String ˊ(double[] dArr) {
        if (dArr.length == 0) {
            return "[]";
        }
        StringBuilder stringBuilder = new StringBuilder(dArr.length << 4);
        stringBuilder.append('[');
        for (double d : dArr) {
            stringBuilder.append(Double.toString(d));
            stringBuilder.append(',');
        }
        stringBuilder.setCharAt(stringBuilder.length() - 1, ']');
        return stringBuilder.toString();
    }

    static String ˏ(short[] sArr) {
        if (sArr.length == 0) {
            return "[]";
        }
        StringBuilder stringBuilder = new StringBuilder(sArr.length << 4);
        stringBuilder.append('[');
        for (short sh : sArr) {
            stringBuilder.append(Short.toString(sh));
            stringBuilder.append(',');
        }
        stringBuilder.setCharAt(stringBuilder.length() - 1, ']');
        return stringBuilder.toString();
    }

    static String ˋ(byte[] bArr) {
        if (bArr.length == 0) {
            return "[]";
        }
        StringBuilder stringBuilder = new StringBuilder(bArr.length << 4);
        stringBuilder.append('[');
        for (byte b : bArr) {
            stringBuilder.append(Byte.toString(b));
            stringBuilder.append(',');
        }
        stringBuilder.setCharAt(stringBuilder.length() - 1, ']');
        return stringBuilder.toString();
    }

    public static String ˊ(Object obj) throws JSONException {
        if (obj == null) {
            return "{}";
        }
        Field[] declaredFields = obj.getClass().getDeclaredFields();
        StringBuilder stringBuilder = new StringBuilder(declaredFields.length << 4);
        stringBuilder.append('{');
        for (final Field field : declaredFields) {
            String str = "";
            if (!field.isAccessible()) {
                AccessController.doPrivileged(new PrivilegedAction() {
                    public Object run() {
                        field.setAccessible(true);
                        return null;
                    }
                });
            }
            if (field.isAnnotationPresent(PostField.class) && field.getType() != obj.getClass()) {
                Object value = ((PostField) field.getAnnotation(PostField.class)).value();
                if (TextUtils.isEmpty(value)) {
                    value = field.getName();
                }
                stringBuilder.append(ˋ(value));
                stringBuilder.append(':');
                try {
                    stringBuilder.append(ˋ(field.get(obj)));
                } catch (IllegalAccessException e) {
                    stringBuilder.append(HwAccountConstants.NULL);
                }
                stringBuilder.append(',');
            }
        }
        if (stringBuilder.length() == 1) {
            return obj.toString();
        }
        stringBuilder.setCharAt(stringBuilder.length() - 1, '}');
        return stringBuilder.toString();
    }
}
